package com.study.talk.app.group;

import android.os.Bundle;

import androidx.recyclerview.widget.LinearLayoutManager;

import com.google.android.material.snackbar.Snackbar;

import com.study.talk.adapter.UserListCheckedListAdapter;
import com.study.talk.app.base.BaseActivity;
import com.study.talk.databinding.ActivityInviteFriendJoinGroupBinding;
import com.study.talk.entity.GroupInfo;
import com.study.talk.entity.UserInfo;
import com.study.talk.entity.resp.QueryUserListResponse;
import com.study.talk.entity.vo.GroupUserInfoVo;
import com.study.talk.netty.NettyClient;
import com.study.talk.netty.message.request.InviteGroupRequest;
import com.study.talk.netty.message.response.InviteGroupSelfResponse;
import com.study.talk.util.DateUtil;
import com.study.talk.net.OkHttp;
import com.study.talk.util.GsonInstance;
import com.study.talk.util.ToastUtil;
import com.study.talk.util.alert.AlertUtil;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;


public class InviteFriendJoinGroupActivity extends BaseActivity {


    private GroupInfo groupInfo;

    private List<UserInfo> friendList;

    private ActivityInviteFriendJoinGroupBinding activityInviteFriendJoinGroupBinding;
    private UserListCheckedListAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activityInviteFriendJoinGroupBinding = ActivityInviteFriendJoinGroupBinding.inflate(getLayoutInflater());
        setContentView(activityInviteFriendJoinGroupBinding.getRoot());
        setSupportActionBar(activityInviteFriendJoinGroupBinding.inviteFriendJoinGroupToolbar);

        groupInfo = GsonInstance.instance.fromJson(getIntent().getStringExtra("data"), GroupInfo.class);

        initView();
    }


    private void initView() {

        activityInviteFriendJoinGroupBinding.inviteFriendJoinGroupToolbar.setNavigationOnClickListener(v -> finish());

        //查询还未在群里的好友
        OkHttp.post("/friend/get/not/in/group/list", GsonInstance.instance.toJson(new GroupUserInfoVo(groupInfo.getId(), getApp().userId())), res -> {
            QueryUserListResponse response = GsonInstance.instance.fromJson(res, QueryUserListResponse.class);
            friendList = response.getData();
            if (friendList == null || friendList.isEmpty()) {
                runOnUiThread(() -> ToastUtil.showToast(InviteFriendJoinGroupActivity.this, "暂无符合要求的好友，快去多交点朋友吧..."));
            } else {
                adapter = new UserListCheckedListAdapter(friendList, this);
                activityInviteFriendJoinGroupBinding.inviteFriendJoinGroupListView.setAdapter(adapter);
                runOnUiThread(() -> {
                    activityInviteFriendJoinGroupBinding.inviteFriendJoinGroupListView.setLayoutManager(new LinearLayoutManager(this));
                    activityInviteFriendJoinGroupBinding.inviteFriendJoinGroupListView.setAdapter(adapter);
                });
            }
        });

        activityInviteFriendJoinGroupBinding.inviteFriendJoinGroupBt.setOnClickListener(view -> {
            List<UserInfo> selectList = new ArrayList<>();
            for (UserInfo userInfo : friendList) {
                if (Boolean.TRUE.equals(adapter.selectMap.get(userInfo.getId()))) {
                    selectList.add(userInfo);
                }
            }
            ToastUtil.showToast(InviteFriendJoinGroupActivity.this, "" + selectList.size());
            InviteGroupRequest inviteGroupRequest = new InviteGroupRequest(groupInfo.getId(), selectList);
            inviteGroupRequest.setDateTime(DateUtil.getCurrentTime());
            NettyClient.sendMessage(inviteGroupRequest);
            showLoadingDialog();
        });
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onInviteGroupSelfResponseMessage(InviteGroupSelfResponse inviteGroupSelfResponse) {
        dismissLoadingDialog();
        Snackbar.make(activityInviteFriendJoinGroupBinding.inviteFriendJoinGroupBt, "邀请发送成功" + inviteGroupSelfResponse.getSuccessUser().size() + "个,失败" + inviteGroupSelfResponse.getFailedUser().size() + "个", Snackbar.LENGTH_SHORT)
                .setAction("查看详情", view -> {
                    AlertUtil.show(InviteFriendJoinGroupActivity.this, "没有详情", "懒得写了");
                }).show();
    }
}